#include <sw/redis++/redis++.h>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    cout << "hset and hget" << endl;
    redis.flushall();

    redis.hset("key", "name", "alpaca");
    redis.hset("key", std::make_pair("info", "222"));
    redis.hset("kqy", {std::make_pair("f3", "333"),
                       std::make_pair("f4", "444")});
    vector<std::pair<std::string, std::string>> fields = {
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666"),
    };
    redis.hset("key", fields.begin(), fields.end());

    auto result = redis.hget("key", "name");
    if (result)
        cout << result.value() << endl;
}

void test2(Redis &redis)
{
    cout << "hexists" << endl;
    redis.flushall();

    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");

    auto ret = redis.hexists("key", "f1");
    cout << ret << endl;
}

void test3(Redis &redis)
{
    cout << "hdel" << endl;
    redis.flushall();

    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");

    auto ret = redis.hdel("key", "f1");
    cout << ret << endl;

    ret = redis.hdel("key", {"f2", "f3"});
    cout << ret << endl;

    auto len = redis.hlen("key");
    cout << len << endl;
}

void test4(Redis &redis)
{
    cout << "hkeys and hvals" << endl;
    redis.flushall();

    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");

    vector<std::string> keys;
    auto it = std::back_inserter(keys);
    redis.hkeys("key", it);

    for (auto e : keys)
        cout << e << endl;

    vector<std::string> values;
    it = std::back_inserter(values);
    redis.hvals("key", it);

    for (auto e : values)
        cout << e << endl;
}

void test5(Redis &redis)
{
    cout << "hmget and hmet" << endl;
    redis.flushall();

    redis.hmset("key", {std::make_pair("f1", "111"),
                        std::make_pair("f2", "222"),
                        std::make_pair("f3", "333")});

    vector<std::pair<std::string, std::string>> pairs = {
        std::make_pair("f4", "444"),
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666"),
    };
    redis.hmset("key", pairs.begin(), pairs.end());

    vector<std::string> results;
    auto it = std::back_inserter(results);
    redis.hmget("key", {"f1", "f4", "f6"}, it);

    for (auto e : results)
        cout << e << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    test5(redis);
    return 0;
}